package com.szm.lnr.base;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.szm.lnr.globle.LarApplication;

/**
 * Created by szm on 2018/7/27.
 */

public class BaseActivity extends AppCompatActivity {

    public static final int SYSTEM_UI_FLAG = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            | View.SYSTEM_UI_FLAG_FULLSCREEN
            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        LarApplication.addActivity(this);
        getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int visibility) {
                hideSystemUi();
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        hideSystemUi();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LarApplication.removeActivity(this);
    }

    /**
     * 隐藏系统虚拟按键等
     */
    @SuppressLint("NewApi")
    public void hideSystemUi() {
        getWindow().getDecorView().setSystemUiVisibility(SYSTEM_UI_FLAG);
        getWindow().setStatusBarColor(Color.TRANSPARENT);
        getWindow().setNavigationBarColor(Color.TRANSPARENT);
    }

    /**
     * 替代findViewById的功能 好处不需要转型
     *
     * @param id id
     * @return view
     */
    public <T extends View> T findTargetViewById(int id) {
        View view = findViewById(id);
        if (view == null) {
            throw new NullPointerException("没有找到您指定的View,请检查当前layout文件是否正确配置");
        }
        return (T) view;
    }

    /**
     * 获取SharedPreferences
     * @return
     */
    public SharedPreferences getShearedPreferences(){
        return getSharedPreferences(getPackageName(), Context.MODE_PRIVATE);
    }
}
